//
//  CinemaListViewController.m
//  豆瓣
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "CinemaListViewController.h"
#import "AppTools.h"
#import "MBProgressHUD.h"
#import "Cinema.h"
#import "CinemaListTableViewCell.h"
@interface CinemaListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray *cinemaArray;
@property (nonatomic, retain) MBProgressHUD *hub;

@end

@implementation CinemaListViewController

- (void)dealloc
{
    [_cinemaArray release];
    [_myTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    self.navigationItem.title = @"电影院";
    
    [self.myTableView registerClass:[CinemaListTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.cinemaArray = [NSMutableArray array];
    
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/cinemalist.php";
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"正在加载中....";
    
    
    [AppTools getDataWithURL:urlStr block:^(id result) {
        
        NSMutableDictionary *dic = result;
        
        NSString *reason = [dic objectForKey:@"reason"];
        
        if ([reason isEqualToString:@"success"]) {
            
            NSDictionary *resultDic = [dic objectForKey:@"result"];
            NSMutableArray *array = [resultDic objectForKey:@"data"];
            
            NSLog(@"%@", array);
            
                for (NSDictionary *dic2 in array) {
                    
                    NSLog(@"%@", dic2);
                    Cinema *cinema = [[Cinema alloc] init];
                    [cinema setValuesForKeysWithDictionary:dic2];
                    [self.cinemaArray addObject:cinema];
                    [cinema release];
                    
                }
            
            [self.myTableView reloadData];
            
            [self.hub hide:YES];
            
        }else{
            
            NSLog(@"请求失败");
            
        }
        
    }];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.cinemaArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellaa = @"cell";
    CinemaListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellaa];
    
    Cinema *cin = [self.cinemaArray objectAtIndex:indexPath.row];
    
    cell.myCinema = cin;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
